package com.sgzn.sgpay.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsSessionQueryRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.persistence.dao.TLoginSessionMapper;
import com.sgzn.sgpay.persistence.dao.TMchLoginSessionMapper;
import com.sgzn.sgpay.persistence.domain.TLoginSession;
import com.sgzn.sgpay.persistence.domain.TLoginSessionExample;
import com.sgzn.sgpay.persistence.domain.TMchLoginSession;
import com.sgzn.sgpay.persistence.domain.TMchLoginSessionExample;
import com.sgzn.sgpay.service.SgsSessionQueryService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会话查询服务
 * @author zhongzhongyong
 * @version Id: SgsSessionQueryServiceImpl.java, v 0.1 2018/6/21 21:03 zhongzhongyong Exp $$
 */
@Service
@Slf4j
public class SgsSessionQueryServiceImpl implements SgsSessionQueryService {

    @Autowired
    private TLoginSessionMapper tLoginSessionMapper;

    @Autowired
    private TMchLoginSessionMapper tMchLoginSessionMapper;

    @Override
    public SgsResultResponse<TLoginSession> queryUserSession(SgsSessionQueryRequest request) {
        try {
            TLoginSessionExample tLoginSessionExample = new TLoginSessionExample();
            tLoginSessionExample.createCriteria().andTokenEqualTo(request.getLoginToken());
            List<TLoginSession> list = tLoginSessionMapper.selectByExample(tLoginSessionExample);
            if (CollectionUtils.isEmpty(list)) {
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseCode(),SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseDesc());
            }
            return SgsResponseUtil.INSTANCE.success(list.get(0));
        }  catch (Exception e) {
            log.error("查询用户会话异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }

    @Override
    public SgsResultResponse<TMchLoginSession> queryMchSession(SgsSessionQueryRequest request) {
        try {
            TMchLoginSessionExample tMchLoginSessionExample = new TMchLoginSessionExample();
            tMchLoginSessionExample.createCriteria().andTokenEqualTo(request.getLoginToken());
            List<TMchLoginSession> list = tMchLoginSessionMapper.selectByExample(tMchLoginSessionExample);
            if (CollectionUtils.isEmpty(list)) {
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseCode(),SgsResponseCode.C_TOKEN_NOT_FOUND.getResponseDesc());
            }
            return SgsResponseUtil.INSTANCE.success(list.get(0));
        }  catch (Exception e) {
            log.error("查询用户会话异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }


}

